package cc.yaomm.common.basic.domain;

import cc.yaomm.common.basic.base.SuperEnum;
import com.alibaba.fastjson.JSON;
import cc.yaomm.common.basic.enums.ResponseCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.io.Serializable;
import java.util.Collection;

/**
 * <p>@Description : 响应消息类</p>
 * <p>@Author : Qilin.Xing </p>
 * <p>@Date : 2017年8月18日 0018 下午 12:02 </p>
 */
@Getter
public class Response<T> implements Serializable {

    protected Response(SuperEnum<?, String> code, T data, String msg, String ex) {
        this.code = code.getValue();
        this.data = data;
        this.msg = msg;
        this.ex = ex;
    }

    public static Response fail(String msg) {
        return new Response(ResponseCode.FAILED, null, msg, null);
    }

    public static Response fail(SuperEnum<?, String> code, String msg) {
        return new Response(code, null, msg, null);
    }

    public static Response fail(SuperEnum<?, String> code, String msg, String ex) {
        return new Response(code, null, msg, ex);
    }

    public static <T extends Serializable> Response success() {
        return new Response(ResponseCode.SUCCESS, null, null, null);
    }

    public static <T extends Serializable> Response success(T data) {
        return new Response(ResponseCode.SUCCESS, data, null, null);
    }

    public static <T extends Serializable> Response success(T data, String msg) {
        return new Response(ResponseCode.SUCCESS, data, msg, null);
    }

    public static <T extends Serializable> Response success(Collection<T> data) {
        return new Response(ResponseCode.SUCCESS, data, null, null);
    }

    public static <T extends Serializable> Response success(Collection<T> data, String msg) {
        return new Response(ResponseCode.SUCCESS, data, msg, null);
    }

    public static <T extends Serializable> Response build(SuperEnum<?, String> code, T data) {
        return new Response(code, data, null, null);
    }

    public static <T extends Serializable> Response build(SuperEnum<?, String> code, T data, String msg) {
        return new Response(code, data, msg, null);
    }

    public static <T extends Serializable> Response build(SuperEnum<?, String> code, Collection<T> data) {
        return new Response(code, data, null, null);
    }

    public static <T extends Serializable> Response build(SuperEnum<?, String> code, Collection<T> data, String msg) {
        return new Response(code, data, msg, null);
    }

    /**
     * 业务状态码，决定业务流程
     */
    @ApiModelProperty(value = "业务状态码，判断业务流程，不允许前端统一拦截判断<br/>" +
            "常见返回值：SUCCESS-成功,FAILED-失败,EXCEPTION-异常,等等其他业务值")
    private String code;

    /**
     * 业务消息
     */
    @ApiModelProperty("业务消息，不建议此消息面向用户")
    private String msg;

    /**
     * 异常信息
     */
    @ApiModelProperty("异常信息[一般用于调试]")
    private String ex;

    /**
     * 数据携带
     */
    @ApiModelProperty("数据携带")
    private T data;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
